package org.kyw.auth.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.kyw.auth.domain.Role;
import org.kyw.auth.query.RoleQuery;
import org.kyw.auth.service.IPermissionService;
import org.kyw.auth.service.IRoleService;
import org.kyw.basic.exception.BusinessException;
import org.kyw.basic.util.AjaxResult;
import org.kyw.basic.util.PageList;
import org.kyw.system.domain.SystemDictionaryType;
import org.kyw.system.query.SystemDictionaryTypeQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 角色管理接口
 */

@RestController
@RequestMapping("/role")
@Api(tags = "角色接口",description = "角色接口详细描述")
public class RoleController {

    @Autowired
    private IRoleService roleService;

    @Autowired
    private IPermissionService permissionService;

    /**
     * 分页 + 高级查询
     * @return
     */
    @PostMapping("/list")
    public PageList<Role> queryPage(@RequestBody RoleQuery query){
        return roleService.queryPage(query);
    }


    //跳转到当前角色授权页面
    @PostMapping("/{id}")
    public AjaxResult queryPermissionByRoleId(@PathVariable("id") Long id){
        try {
            Map<String,Object> map = permissionService.queryPermissionByRoleId(id);
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult
                    .me()
                    .setSuccess(false)
                    .setMessage("报错了，正在殴打程序员");
        }
    }

     //给当前角色授权
//    @RequestMapping("/addPermission")
//    public AjaxResult addPermission(Long roleId,Long[] permissionIds){
//        Map<String,Object> map = new HashMap<>();
//        try {
//            roleService.addPermission(roleId,permissionIds);
//            map.put("success", true);
//            map.put("msg", "操作成功");
//        } catch (Exception e) {
//            e.printStackTrace();
//            map.put("success", true);
//            map.put("msg", "系统繁忙，请稍后");
//        }
//        return map;
//    }
}
